package com.geostar.gmp.snmp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import com.geostar.gmp.exception.PDUExecption;
/**
 * 
 * 文件名:SnmpGet.java
 * 功能:
 * 目前支持方法
 * --------------------------------------
 * 初始版本 
 * 作者: yesong
 * 时间: Mar 31, 2014
 */
public class SnmpGet {

	private static Logger LOGGER = LoggerFactory.getLogger(SnmpGet.class);
	/** SNMP版本 **/
	public static final int DEFAULT_VERSION = SnmpConstants.version2c;
	/** 协议，默认为Udp **/
	public static final String DEFAULT_PROTOCOL = "udp";
	/** 端口 **/
	public static final int DEFAULT_PORT = 161;
	/** 默认超时时间 **/
	public static final long DEFAULT_TIMEOUT = 1 * 1000L;
	/** 默认重试此次数 **/
	public static final int DEFAULT_RETRY = 1;
	/** 默认访问权限 **/
	public static final String DEFAULT_PUBLIC = "public";

	/**
	 * 创建对象communityTarget
	 *
	 * @param targetAddress
	 * @param community
	 * @param version
	 * @param timeOut
	 * @param retry
	 * @return CommunityTarget
	 */
	private static CommunityTarget createTarget(String ip) {
		Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip
				+ "/" + DEFAULT_PORT);
		CommunityTarget target = new CommunityTarget();
		target.setCommunity(new OctetString(DEFAULT_PUBLIC));
		target.setAddress(address);
		target.setVersion(DEFAULT_VERSION);
		target.setTimeout(DEFAULT_TIMEOUT); // milliseconds
		target.setRetries(DEFAULT_RETRY);
		return target;
	}
	
	/**
	 * @param ip
	 * @param community
	 * @param oid
	 */
	public static ResponseEvent snmpGetList(String ip,List<String> oidList) {
		CommunityTarget target = createTarget(ip);
		Snmp snmp = null;
		try {
			PDU pdu = new PDU();

			for (String oid : oidList) {
				pdu.add(new VariableBinding(new OID(oid)));
			}
			DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping();
			transport.listen();
			snmp = new Snmp(transport);
			LOGGER.info("------->发送消息<-------");
			pdu.setType(PDU.GET);
			ResponseEvent respEvent = snmp.send(pdu, target);
			LOGGER.info("PeerAddress:" + respEvent.getPeerAddress());
			return respEvent;
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.info("SNMP GetList Exception:" + e);
		} finally {
			if (snmp != null) {
				try {
					snmp.close();
				} catch (IOException ex1) {
					snmp = null;
				}
			}
		}
		return null; 
	}

	public static boolean isLinkd(String ip) throws PDUExecption{
		CommunityTarget target = createTarget(ip);
		Snmp snmp = null;
		ResponseEvent respEvent = null;
		try {
			PDU pdu = new PDU();
			pdu.add(new VariableBinding(new OID("")));
			DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping();
			transport.listen();
			snmp = new Snmp(transport);
			LOGGER.info("------->发送测试连接消息<-------");
			pdu.setType(PDU.GET);
			respEvent = snmp.send(pdu, target);
//			PDU response = respEvent.getResponse();
//			for (int i = 0; i < response.size(); i++) {
//				VariableBinding  vb = response.get(i);
//				LOGGER.info("OID:" + vb.getOid() + "--variable:" + vb.getVariable());
//			}
		} catch (Exception ex){
			ex.printStackTrace();
		} 
		finally {
			if (snmp != null) {
				try {
					snmp.close();
				} catch (IOException ex1) {
					snmp = null;
				}
			}
		}
		return respEvent != null && respEvent.getResponse() != null;
	}
	
	/**
	 * 目的:测试
	 * @param args
	 */
	public static void main(String[] args) {
		String ip = "192.168.1.0";
		List<String> oidList = new ArrayList<String>();
//		oidList.add(".1.3.6.1.2.1.1.5.0");
//		oidList.add(".1.3.6.1.2.1.1.3.0");
//		oidList.add(".1.3.6.1.2.1.1.5.0");
		
		/** cpu **/
//		oidList.add("1.3.6.1.2.1.25.3.3.1.2.2");
//		oidList.add("1.3.6.1.2.1.25.3.3.1.2.3");
//		oidList.add("1.3.6.1.2.1.25.3.3.1.2.4");
//		oidList.add("1.3.6.1.2.1.25.3.3.1.2.5");
		
		
//		oidList.add("1.3.6.1.2.1.25.3.3.1.2.6");
//		oidList.add("1.3.6.1.2.1.25.3.3.1.2.7");
//		oidList.add("1.3.6.1.2.1.25.2.2.0");
		
//		oidList.add("1.3.6.1.2.1.25.2.3.1.3.1");
//		oidList.add("1.3.6.1.2.1.25.2.3.1.3.2");
//		oidList.add("1.3.6.1.2.1.25.2.3.1.3.3");
//		oidList.add("1.3.6.1.2.1.25.2.3.1.3.4");
		
//		ResponseEvent responseEvent = SnmpGet.snmpGetList(ip, oidList);
//		PDU response = responseEvent.getResponse();
//		for (int i = 0; i < response.size(); i++) {
//			VariableBinding vb = response.get(i);
//			LOGGER.info("OID:" + vb.getOid() + "--VB:" + vb.getVariable());
//		}
		try {
			System.out.println(SnmpGet.isLinkd(ip));
		} catch (PDUExecption e) {
			e.printStackTrace();
		}
	}

}